package com.lianjing.sh.acceleo.controller.tool.query;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.emf.common.util.EList;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.internal.impl.ClassImpl;

import com.lianjing.sh.acceleo.controller.tool.service.NameService;

public class Class_Common{
	
	private static final NameService Name_Service = new NameService();

	/**
	 * 的到全部继承者
	 */
	public List<Classifier> getAllGenerals(Classifier aClass){
		return getGenerals(aClass.getGenerals(), new ArrayList<Classifier>());
	}

	private List<Classifier> getGenerals(EList<Classifier> generals, List<Classifier> list){
		if(!generals.isEmpty()){
			for(Classifier item : generals){
				getGenerals(item.getGenerals(), list);
				list.add(item);
			}
		}
		return list;
	}
	
	/**
	 * 去掉重复的成员变量，当前的覆盖继承的
	 */
	public List<Property> clearRepeatProperty(List<Property> properties){
		Map<String, Property> map = new LinkedHashMap<>();
		for(Property p : properties){
			map.put(Name_Service.getSimpleName(p), p);
		}
		//
//		List<Property> ps = CollectionUtil.createNewSequence(); 
		List<Property> ps = new ArrayList<>();
		for(Property value : map.values()){
			ps.add(value);
		}
		return ps;
	}
	
	/**
		[query public isPropertyField(p:Property) : Boolean = 
			p.isStatic._not() and p.visibility.toString()='private'
		/]
	 */
	public Boolean isPropertyField(Property p){
		return !p.isStatic();
//		return !p.isStatic() && (p.getVisibility() == VisibilityKind.PRIVATE_LITERAL);
	}
	
	/**
		[query private getDirectedPropertyFields(aClass:Classifier) : Sequence(Property) = 
			aClass.attribute->asSequence()->select(p:Property| p.isPropertyField())
		/]
	*/
	public List<Property> getDirectedPropertyFields(Classifier aClass){
		List<Property> list = new ArrayList<>();
		for(Property p : aClass.getAttributes()){
			if(isPropertyField(p)){
				list.add(p);
			}
		}
		return list;
	}
	
	/**
		[query public isClassPropertyField(p : Property) : Boolean = 
			p.isPropertyField() and p.type.oclIsTypeOf(Class)
		 /]
	 */
	public Boolean isClassPropertyField(Property p){
		return isPropertyField(p) && p.getType().getClass()==ClassImpl.class;
	}
	
}
